import { Ball } from "./_ball";

//砖块
export class Block {
  width = 70;
  height = 20;
  alive = true;
  lifes = 2;
  constructor(public x: number, public y: number) {}
  kill() {
    this.lifes--;
    if (this.lifes < 1) this.alive = false;
  }
  collide(b: Ball) {
    //两个矩形中心点 x轴距离 <= 两个矩形宽和的一半 &&
    //两个矩形中心点 y轴距离 <= 两个矩形高和的一半
    const a = this;
    const cond1 = [a.x + a.width / 2, a.y + a.height / 2];
    const cond2 = [b.x + b.width / 2, b.y + b.height / 2];
    const cx = Math.abs(cond1[0] - cond2[0]);
    const cy = Math.abs(cond1[1] - cond2[1]);
    if (cx <= (a.width + b.width) / 2 && cy <= (a.height + b.height) / 2) {
      return true;
    }
    return false;
  }
}
